package UI;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


import VideoTools.Movement;
import VideoTools.ThreadVideo;
import VideoTools.VideoPlayer;

import dimension2.Circle;

public class Visualisation extends javax.swing.JPanel implements MouseListener {

	//	memory buffer
	private Graphics buffer;
	//	 memory image corresponding to the graphics buffer
	private Image image;
	// point clicked
	private Point point;
	
    
	
	public Visualisation(Container container) {
		this.setSize(VideoPlayer.size_X, VideoPlayer.size_Y+VideoPlayer.size_Y/10);
		
		if (container==null){
			JFrame jframe = new JFrame();
			jframe.add(this);
			jframe.setSize(VideoPlayer.size_X, VideoPlayer.size_Y);
			jframe.setVisible(true);
			jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		}
		else {
			container.setSize(VideoPlayer.size_X, VideoPlayer.size_Y);
			container.add(this);
		}
				
		this.addMouseListener(this);
		image = createImage(VideoPlayer.size_X, VideoPlayer.size_Y);
		buffer = image.getGraphics();
	}
	
	public void paint(Graphics g){
		super.paint(g);
		g.drawImage(image, 0, 0,this.getSize().width,this.getSize().height, this);
	}

	
	/** draw all if any is null
	 * @param image
	 * @param circle
	 * @param movement
	 * @param frameRate
	 */
	public void paintFrame(Image image, Circle[] circle, Movement[] movement,double frameRate) {
		if (image!=null){
			buffer.drawImage(image, 0,0,this);
		}
		else {
			buffer.setColor(Color.red);
			buffer.drawString("Initialisalise the Video... ", VideoPlayer.size_X/3, VideoPlayer.size_Y/2);
			}
		
		for (int i=0;i<circle.length;i++){
			if (circle[i]!=null){
				buffer.setColor(Color.red);
				circle[i].draw(buffer);
			}

			buffer.drawString("fps : " + frameRate, 10, 220);
			if (movement[i]!=null){
				buffer.setColor(Color.yellow);
				//buffer.drawString(movement[i].toString(), 10, 20);
				int OneThirdX = VideoPlayer.size_X/3;
				int OneThirdY = VideoPlayer.size_Y/3;
				switch(movement[i].getDirection()){
				case UP:buffer.fillRect(OneThirdX, 0, OneThirdX, OneThirdY);break;
				case DOWN:buffer.fillRect(OneThirdX, 2*OneThirdY, OneThirdX, OneThirdY);break;
				case LEFT:buffer.fillRect(0, OneThirdY, OneThirdX, OneThirdY);break;
				case RIGHT:buffer.fillRect(2*OneThirdX, OneThirdY, OneThirdX, OneThirdY);break;
				case UPRIGHT:buffer.fillRect(2*OneThirdX, 0, OneThirdX, OneThirdY);break;
				case DOWNRIGHT:buffer.fillRect(2*OneThirdX, 2*OneThirdY, OneThirdX, OneThirdY);break;
				case UPLEFT:buffer.fillRect(0, 0, OneThirdX, OneThirdY);break;
				case DOWNLEFT:buffer.fillRect(0, 2*OneThirdY, OneThirdX, OneThirdY);break;
				}
			}
		}

		
		repaint();
	}

	public Point getPointClicked(){
		Point point2=point;
		point=null;
		return point2;
	}
	
	public void mouseClicked(MouseEvent arg0) {
		int point_X=(int) (arg0.getX()*(VideoPlayer.size_X/this.getSize().getWidth()));
		int point_Y=(int) (arg0.getY()*(VideoPlayer.size_Y/this.getSize().getHeight()));
		point = new Point(point_X,point_Y);
	}

	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	

	
	
}
